<?php
/**
 * Created by JetBrains PhpStorm.
 * User: scala
 * Date: 12-10-25
 * Time: 上午10:03
 * To change this template use File | Settings | File Templates.
 */
class SuccessCaseAction extends CommonAction
{

    function index(){
        $successcase=M("SuccessCase");
        import("ORG.Util.Page");// 导入分页类
        $count=$successcase->count();
        $Page= new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfigArray(array('header'=>'条成功案例','prev'=>'<input type="button" value="<<<">','next'=>'<input type="button" value=">>>">','first'=>'首页','last'=>'尾页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页   %first% %prePage% %upPage%   <span style="color: red;background-color: #7fffd4;">%linkPage%</span>   %downPage% %nextPage% %end%'));
        $show=$Page->show();//分页显示输出
        $list=$successcase->order("id desc")->limit($Page->firstRow.",".$Page->listRows)->select();
        $this->assign("list",$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }


    function add(){
        $caseType=M("CaseType");
        $typeList=$caseType->select();
        $this->assign("typeList",$typeList);
        $this->display();
    }


    function insert(){
        $case=D("SuccessCase");
        if($case->create()){
            if($result=$case->add()){
                $this->sitemapWriter("SuccessCase/detail","SuccessCase",$result);
                $this->redirect("SuccessCase/index");
            }else{
                $this->error("添加失败".$case->getError());
            }
        }else{
            $this->error("添加信息失败!".$case->getError());
        }
    }


    function  delete(){
        $news=M("SuccessCase");
        if(!empty($_GET["id"])){
            $news->delete($_GET["id"]);
            $this->sitemapDeleter("SuccessCase",$_GET['id']);
        }
        $this->redirect("SuccessCase/index");
    }


    function  view(){
        $news=M("SuccessCase");
        if(!empty($_GET["id"])){
            $list= $news->find($_GET["id"]);
            $this->assign("list",$list);
            $newsType=M("CaseType");
            $typeList=$newsType->select();
            $this->assign("typeList",$typeList);
        }
        $this->display();
    }


    function update(){
        $news=D("SuccessCase");
        if($news->create()){
            $news->save();
        }
        $this->redirect("SuccessCase/index");
    }

    function upload(){
        $news=M("SuccessCase");
        if(!empty($_GET["id"])){
            $list= $news->find($_GET["id"]);
            $this->assign("list",$list);
            $caseImg=M("CaseImg");
            $imgList=$caseImg->where("caseid=".$_GET["id"])->select();
            $this->assign("imgList",$imgList);
        }
        $this->display();
    }


    function up(){
        if(empty($_FILES)){
            $this->error("没有选择要上传的文件！");
        }
        if(empty($_POST["id"])){
            $this->error("上传失败！");
        }
        import("ORG.Net.UploadFile");
        $savepath='./Public/Uploads/successcase/'.$_POST["id"]."/";
        if(!file_exists($savepath)){
            mkdir($savepath);
        }
        $fileUpload=new UploadFile();
        $fileUpload->maxSize=1000000;
        $fileUpload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $fileUpload->allowTypes=array("image/jpg","image/gif","image/png","image/jpeg");// 上传文件的类型
        $fileUpload->savePath =  $savepath;// 设置附件上传目录
        $fileUpload->uploadReplace=true;//是否覆盖，默认的false
        $fileUpload->saveRule="uniqid";
        $fileUpload->thumb=true;//对图片进行缩略处理
        //设置需要生成缩略图的文件后缀
        $fileUpload->thumbPrefix        = 'm_,s_';  //生产2张缩略图

        //设置缩略图最大宽度
        $fileUpload->thumbMaxWidth      = '650,116';
        //设置缩略图最大高度
        $fileUpload->thumbMaxHeight     = '400,136';

        $fileUpload->thumbRemoveOrigin=true;
//        $fileUpload->autoSub=true;
        if(!$fileUpload->upload()) {// 上传错误提示错误信息
            $this->error($fileUpload->getErrorMsg());
        }else{// 上传成功 获取上传文件信息
            //取得成功上传的文件信息
            $uploadList = $fileUpload->getUploadFileInfo();
            import('ORG.Util.Image');
            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
            Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], './Public/Images/logo.gif',null,10);
            $caseImg=M("CaseImg");
            for($i=0;$i<count($uploadList);$i++){
                $data["filename"]=$uploadList[$i]["savename"];
                $data["createtime"]=time();
                $data["caseid"]=$_POST["id"];
                $data["mainimg"]=0;
                $data["sort"]=0;
                $caseImg->data($data)->add();
            }
            $this->redirect("SuccessCase/upload",array("id"=>$_POST["id"]));
        }

    }
}
